import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@abp/components';
import {
  ArticleInfoServiceProxy,
  GetDataComboItemDtoOfInt64,
  ArticleInfoListDto,
  SwitchEntityInputDtoOfInt64,
} from '@abp/service-proxies/service-proxies';
import { CreateOrEditArticleComponent } from './create-or-edit-article/create-or-edit-article.component';

@Component({
  templateUrl: './article.component.html',
  styleUrls: ['./article.component.less'],
})
export class ArticleComponent extends AppComponentBase {
  /** 是否显示高级过滤 */
  advancedFiltersVisible = false;
  /** 搜索条件 */
  keyword: string;
  selectedCloumn: GetDataComboItemDtoOfInt64;
  createStartToEndDate = [];
  updateStartToEndDate = [];
  isEnable: boolean = undefined;
  isAuthorize: boolean = undefined;
  expandDataCache = {};

  constructor(
    injector: Injector,
    private articleService: ArticleInfoServiceProxy,
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.searchData();
  }

  /**
   * 刷新表格数据
   * @param args
   */
  refresh(): void {
    this.searchData();
  }

  sort(sort: { key: string; value: string }): void {
    this.tableHelper.sort = sort;
    this.searchData();
  }

  dateRangeChange() {
    this.searchData();
  }

  isEnableChange() {
    this.searchData();
  }

  isisAuthorizeChange() {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    if (reset) {
      this.tableHelper.pageIndex = 1;
    }
    this.tableHelper.isLoading = true;

    console.log(this.selectedCloumn);
    console.log(this.isEnable);

    this.articleService
      .getArticleInfos(
        false,
        this.selectedCloumn == undefined
          ? undefined
          : this.selectedCloumn.value,
        this.createStartToEndDate.length === 2
          ? this.createStartToEndDate[0] === null
            ? undefined
            : this.createStartToEndDate[0]
          : undefined,
        this.createStartToEndDate.length === 2
          ? this.createStartToEndDate[1] === null
            ? undefined
            : this.createStartToEndDate[1]
          : undefined,
        this.updateStartToEndDate.length === 2
          ? this.updateStartToEndDate[0] === null
            ? undefined
            : this.updateStartToEndDate[0]
          : undefined,
        this.updateStartToEndDate.length === 2
          ? this.updateStartToEndDate[1] === null
            ? undefined
            : this.updateStartToEndDate[1]
          : undefined,
        this.keyword,
        this.isEnable === null ? undefined : this.isEnable,
        this.isAuthorize === null ? undefined : this.isAuthorize,
        this.tableHelper.getSorting(),
        this.tableHelper.getMaxResultCount(),
        this.tableHelper.getSkipCount(),
      )
      .subscribe(data => {
        this.tableHelper.isLoading = false;
        this.tableHelper.totalRecordsCount = data.totalCount;
        this.tableHelper.records = data.items;
      });
  }

  clickIsActiveSwitch(data: ArticleInfoListDto): void {
    const input = new SwitchEntityInputDtoOfInt64();
    input.id = data.id;
    input.switchValue = !data.isActive;
    data.isActive = !data.isActive;
    this.articleService.updateIsActiveSwitchAsync(input).subscribe(result => {
      if (data.isActive) {
        this.notify.success('启用成功!');
      } else {
        this.notify.success('关闭成功!');
      }
    });
  }

  clickIsNeedAuthorizeAccessSwitch(data: ArticleInfoListDto): void {
    const input = new SwitchEntityInputDtoOfInt64();
    input.id = data.id;
    input.switchValue = !data.isNeedAuthorizeAccess;
    data.isNeedAuthorizeAccess = !data.isNeedAuthorizeAccess;
    this.articleService
      .updateIsNeedAuthorizeAccessSwitchAsync(input)
      .subscribe(result => {
        if (data.isActive) {
          this.notify.success('启用成功!');
        } else {
          this.notify.success('关闭成功!');
        }
      });
  }

  createBuildingPlan(): void {
    let config = this.drawerConfig(
      '添加文章',
      CreateOrEditArticleComponent,
      {
        id: undefined,
      },
      500,
    );
    const drawerRef = this.drawerService.create(config);
    drawerRef.afterOpen.subscribe(() => {});
    drawerRef.afterClose.subscribe(data => {
      if (data) {
        this.searchData();
      }
    });
  }

  editBuildingPlan(data: ArticleInfoListDto): void {
    let config = this.drawerConfig(
      '修改文章:' + data.title,
      CreateOrEditArticleComponent,
      {
        id: data.id,
      },
      500,
    );
    const drawerRef = this.drawerService.create(config);
    drawerRef.afterOpen.subscribe(() => {});
    drawerRef.afterClose.subscribe(data => {
      if (data) {
        this.searchData();
      }
    });
  }

  deleteConfirm(id: number): void {
    this.articleService.deleteArticleInfo(id).subscribe(() => {
      this.searchData();
      this.notify.success('删除成功！');
    });
  }

  getRecommendedTypeText(value: number) {
    return RecommendedTypeEnum[value];
  }
}

//定义列表枚举字段，以便友好化展示
enum RecommendedTypeEnum {
  '置顶' = 0,
  '热门' = 1,
  '推荐' = 2,
  '普通' = 3,
}
